<?php
/**
 * Created By Project
 * Author: 陈东
 * Copyright：陈东
 * Source Code：陈东
 * Explain：I shall not authorize for any other commercial purposes, law breakers must be prosecuted.
 */
namespace Home\Controller;
use Think\Controller;

class JgdrawController extends BaseController {
	
	/*
	 * 发送手机验证码
	 */
	public function verifynote(){
		$tel = I('post.usertel');
		$fun= new \Org\Util\fun();
		$tmppwd = substr(mt_rand()."000000",0,6);
		$ssmmess = $fun->SendSms($tel,$tmppwd);
		session('verftel',null);
		session('verftel',$tmppwd);
		//echo $tmppwd;
	}

	public function savetel1(){
		$telres = D(C('TABLE_TELINFO'));
		$tel = I('post.usertel');
		$num = $telres->where(array('tel'=>$tel))->find();
		if($num){
			echo $num['hbnum'];
		}else{
			echo 'no';//不存在
		}
	}

	//判断是否手机号是否被注册过
	public function isreginfo($tel=null){
		$tels = $tel;
		$telres = D(C('TABLE_APPLY'));
		if(empty($tel)){
			$tel = I('post.usertel');
		}
		$num = $telres->where(array('openid'=>$_COOKIE['user_userinfo']))->count();
		if($num > 0){
			return $this->checkout($tels,11);		//一个微信账号只能注册一个手机号
		}else{
			$num1 = $telres->where(array('mobile'=>$tel,'openid'=>$_COOKIE['user_userinfo']))->count();
			if($num1 > 0){
				return $this->checkout($tels,22);	//手机号已注册
			}else{
				return $this->checkout($tels,33);	//未注册
			}
		}

	}


	//保存注册信息
	public function addresreg(){
		$name = I('post.name');
		$mobile = I('post.mobile');
		$verifynote = I('post.verifynote');
		$fakecode = I('post.fakecode');
		$verif = $this->getverifynote($verifynote);
		$result = array();
		if($verif==2){
			//echo 777;									//验证码过期
			$result['ret'] = 777;
		}else if($verif==0){
			//echo 888;									//验证码不正确
			$result['ret'] = 888;
		}else if($verif==1){							//通过验证，紧接着继续判断
			if($this->isreginfo($mobile)==22){			//手机号已注册
				//echo 666;
				$result['ret'] = 666;
			}else if($this->isreginfo($mobile)==11){	//一个微信账号只能注册一个手机号
				//echo 555;
				$result['ret'] = 555;
			}else if($this->isreginfo($mobile)==33){
				$telres = D(C('TABLE_APPLY'));
				$data['mobile']			= $mobile;
				$data['name']			= $name;
				$data['fakecode']		= $fakecode;
				$data['openid']			= $_COOKIE['user_userinfo'];
				$data['applydate']		= date("Y-m-d H:i:m");
				$res = $telres->add($data);
				if($res){
					//echo 111;							//报名成功
					$result['ret'] = 111;
				}else{
					//echo 000;							//报名失败
					$result['ret'] = 000;
				}
			}
		}
		echo json_encode($result);


	}

	//判断验证码是否正确
	public function getverifynote($userpass=null){
		if(!empty(session('verftel'))){
			if($userpass==session('verftel')){
				return 1; //通过
			}else{
				return 0; //不正确
			}
		}else{
			return 2;//验证码过期
		}
	}

	protected function checkout($data,$result){
		if(!empty($data)){
			return $result;
		}else{
			echo $result;
		}
	}

	//倒计时页面
	public function countdown(){

		$this->display('index/countdown');
	}
	//开奖逻辑
	public function kaijiang(){

//		$award = array(
//			array('awardnum'=>5,'id'=>39),
//			array('awardnum'=>3,'id'=>40),
//			array('awardnum'=>2,'id'=>41),
//			array('awardnum'=>1,'id'=>42),
//		);
		$award = $this->getJgwardinfo(38);
		//echo $award[0]['awardnum'];die; //取值方式

		$applys = D(C('TABLE_APPLYS'));
		$cishu = $applys->max('awarpnum');
		if(empty($cishu) || $cishu<1){
			$cishu = 1;
		}else{
			$cishu = $cishu+1;
		}
		//如果不是最后一轮才执行
		if($cishu<=4){
			$executestatus = $this->kj($award[$cishu-1]['awardnum'],$award[$cishu-1]['id'],$cishu);
			//echo $executestatus;
			if($executestatus==1){
				return 1;
			}
		}else{
			return 0;
		}
		//$res = array('count'=>$count);
		//echo json_encode($res);
	}
	/*
	 * 复制中奖的信息到奖池表中
	 * $limit		多少个奖品
	 * $awardid		奖品ID号
	 */
	protected function kj($limit,$awardid,$lqnum){
		$arrid = null;
		$dataList = array();
		$apply = D(C('TABLE_APPLY'));
		$res = $apply->where(array('status'=>0))->order('rand()')->limit($limit)->select();
		if($res){
			foreach($res as $r){
//				if(date("Y-m-d",$r['applydate'])=="2016-09-27"){
//
//				}
				$arrid .= $r['id'].',';
				$dataList[] = array(
						'mobile'=>$r['mobile'],
						'name'=>$r['name'],
						'fakecode'=>$r['fakecode'],
						'applydate'=>$r['applydate'],
						'status'=>0,
						'awardid'=>$awardid, //奖品ID
						'awarpnum'=>$lqnum,
						'awarpdate'=>date("Y-m-d H:i:m"),
						'openid'=>$r['openid']
				);
			}
			$arrid = rtrim($arrid,',');
			//改变已中过奖的名单 status=1
			$changestatus = $this->randsavedata($arrid,$lqnum,1);
			if($changestatus > 0){
				$result = $this->getdatatoadddata($dataList);
				if($result){
					//发送中奖短信提醒
					foreach($dataList as $v){
						$mobile = $v['mobile'];
						$awardname = $this->getawardinfo($v['awardid'],'awardname');
						//echo $mobile.'-'.$awardname;
						$this->sendawardtelinfo($mobile,$awardname);
					}
					$dataList = null;
					//复制成功
					echo 1;
				}
			}
		}
	}

	//发送中奖短信提醒
	protected function sendawardtelinfo($mobile,$awardname){
		$fun= new \Org\Util\fun();
		$fun->Sendawardms($mobile,$awardname);
	}

	//根据奖品ID查询奖品信息
	protected function getawardinfo($id,$field){
		$award = D(C('TABLE_AWARD'));
		$res = $award->where(array('id'=>$id))->find();
		if($res){
			return $res[$field];
		}
	}

	//获取金港的奖品信息
	protected function getJgwardinfo($id){
		$award = D(C('TABLE_AWARD'));
		$res = $award->field('id,awardnum')->where(array('typeid'=>$id))->select();
		return $res;
	}
	//根据获取过来的数据插入指定的数据表中
	protected function getdatatoadddata($data){
		$applys = D(C('TABLE_APPLYS'));
		$query = $applys->addAll($data);
		return $query;
	}
	//改变中奖状态，设为1
	protected function randsavedata($arrid,$lqnum,$value){
		$apply = D(C('TABLE_APPLY'));
		$where['id'] = array('in',$arrid);
		$data['status'] = $value;
		$data['awarpnum'] = $lqnum;
		$res = $apply->where($where)->save($data);
		return $res;
	}

}